<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <meta name="layout" content="grame"/>
    <title>系统日志管理</title>
</head>

<body>
<div class="container" ng-app="logger" ng-controller="LoggerController">
    <div class="panel">
        更改后即时生效（重启后还原）。保存后永久生效。
    </div>

    <div class="row">
        <div class="form-inline">
            <label for="app_logger">App Logger</label>
            <select id="app_logger" ng-model="level">
                <option value="ERROR">ERROR</option>
                <option value="INFO">INFO</option>
                <option value="DEBUG">DEBUG</option>
                <option value="TRACE">TRACE</option>
            </select>
        </div>
    </div>

    <div class="row">
        <div class="form-inline">
            <label for="hibernate_logger">Hibernate Logger</label>
            <select id="hibernate_logger" ng-model="dbLevel">
                <option value="ERROR">ERROR</option>
                <option value="INFO">INFO</option>
                <option value="DEBUG">DEBUG</option>
                <option value="TRACE">TRACE</option>
            </select>
        </div>
    </div>

    <div class="row">
        <button ng-click="logger.test()">测试</button>
        <button ng-click="logger.save()">保存</button>
    </div>
</div>
<content tag="script">
    <script>
        (function () {
            'use script';
            angular.module("logger", [])
                    .controller("LoggerController", ['$scope', '$http', '$templateCache',
                        function ($scope, $http, $templateCache) {
                            var self = this;
                            $http.get('${createLink(controller: 'logger',action: 'show')}',
                                    {params: {logger: 'grame'}, cache: $templateCache})
                                    .then(function successCallback(response) {
                                        console.log(response);
                                        var level = response.data.level;
                                        $scope.level = level;
                                        self.level = level;
                                    },
                                    function errorCallback(response) {
                                        console.log(response);
                                        // ignore
                                    });
                            $http.get('${createLink(controller: 'logger',action: 'show')}',
                                    {params: {logger: 'org.hibernate.SQL'}, cache: $templateCache})
                                    .then(function successCallback(response) {
                                        console.log(response);
                                        var level = response.data.level;
                                        $scope.dbLevel = level;
                                        self.dbLevel = level;
                                    },
                                    function errorCallback(response) {
                                        console.log(response);
                                        // ignore
                                    });
                            $scope.$watch('level', function setLevel() {
                                var level = $scope.level;
                                if (typeof(level) != 'undefined' && self.level != level) {
                                    console.log("do function setLevel(" + level + ")");
                                    update('conf', level);
                                    update('grails.app.controllers', level);
                                    update('grails.app.domain', level);
                                    update('grails.app.services', level);
                                    update('grails.app.filters', level);
                                    update('grame', level);
                                    update('com.springca', level);
                                    if (level == 'DEBUG') {
                                        update('grails.app.taglib', 'INFO');
                                    }
                                    self.level = level;
                                }
                            });
                            $scope.$watch('dbLevel', function setHibernateLevel() {
                                var level = $scope.dbLevel;
                                if (typeof(level) != 'undefined' && self.dbLevel != level) {
                                    console.log("do function setHibernateLevel(" + level + ")");
                                    update('org.hibernate.SQL', level);
                                    if (level == 'DEBUG') {
                                        update('org.hibernate.type.descriptor.sql.BasicBinder', 'TRACE');
                                    } else {
                                        update('org.hibernate.type.descriptor.sql.BasicBinder', level);
                                    }
                                    self.dbLevel = level;
                                }
                            });
                            function update(logger, level) {
                                console.log("update logger:" + logger + ", level:" + level);
                                $http.post('${createLink(controller: 'logger',action: 'update')}',
                                        {logger: logger, level: level},
                                        {cache: $templateCache})
                                        .then(function successCallback(response) {
                                            console.log(response);
                                            // ignore
                                        },
                                        function errorCallback(response) {
                                            console.log(response);
                                            // ignore
                                        });
                            }

                            // 向服务端发起测试请求，服务端会在LoggerController进行日志输出测试
                            self.test = function test() {
                                $http.get('${createLink(controller: 'logger',action: 'test')}');
                            };

                            self.save = function save() {
                                $http.post('${createLink(controller: 'logger',action: 'save')}');
                            }
                        }]);
        })();
    </script>
</content>
</body>
</html>